package com.gai.shop.service.impl;

import com.gai.shop.dao.CartDao;
import com.gai.shop.dao.impl.CartDaoImpl;
import com.gai.shop.entity.Cart;
import com.gai.shop.entity.Product;
import com.gai.shop.service.CartService;
import com.gai.shop.service.ProductService;

import java.util.List;

public class CartServiceImpl implements CartService {
    private final CartDao CD = new CartDaoImpl();

    /**
     * 操作加入购物车
     *    1新增 2修改
     * @param uid
     * @param pid
     */
    @Override
    public void insertIntoCartByUidAndPid(Integer uid, Integer pid) {
        Cart cart = null;
        try {
            //1查询数据 然后判断用户的购物车中是否已有该商品的数据存在
            cart = CD.selectCartProductByUidAndPid(uid, pid);
            //2判断购物车是否为null
            if (cart != null) {
                //购物车已有该数据存在
                //修改
                //先设置商品数量
                cart.setCnum(cart.getCnum() + 1);
                //再设置商品小计
                cart.setCcount(cart.getProduct().getPprice() * cart.getCnum());
                //修改原数据 数量加一 及小计重新计算为(数量*单价)
                CD.updateCartSetCnumAndCcount(cart);
            }else {
                cart = new Cart();
                //使用pid来查询对应的商品，用户保存我们购物车信息
                ProductService ps = new ProductServiceImpl();
                Product product = ps.selectProductByPid(pid);
                //新加入购物车的商品数量起始为1
                cart.setCnum(1);
                cart.setProduct(product);
                cart.setCcount(cart.getCnum() * product.getPprice());//商品数量*单价计算小计
                cart.setUid(uid);
                CD.insertIntoCart(cart);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过uid来查找是否有购物车数据存在
     * @param uid
     * @return
     */
    @Override
    public List<Cart> selectCartProductByUidAndPid(Integer uid) {
        List<Cart> cartList = null;
        try {
            if (uid>0) {
                cartList = CD.selectCartProductByUidAndPid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cartList;
    }

    /**
     * 删除或清空购物车
     * @param uid
     * @param pid
     */
    @Override
    public void deleteCartByUid(Integer uid, Integer pid) {
        try {
            //pid==0的时候就是清空购物车数据
            if (pid == 0){
                CD.deleteCartByUid(uid);
            }else {
                //删除单个购物车数据 uid+pid
                CD.deleteCartByUid(uid, pid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 购物车数据修改
     * @param cart 购物车小计
     * @return
     */
    @Override
    public Integer updateCartSetCnumAndCcount(Cart cart) {
        Integer result = 0;
        try {
            if (cart != null) {
                result = CD.updateCartSetCnumAndCcount(cart);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过uid和pid来查找是否有购物车数据存在
     * @param uid
     * @param pid
     * @return
     */
    @Override
    public Cart selectCartProductByUidAndPid(Integer uid, Integer pid) {
        try {
            return CD.selectCartProductByUidAndPid(uid, pid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
